import 'dart:math';

import 'package:flutter/material.dart';
import 'package:whale/helper/utils/util.dart';
import 'package:whale/pages/im/im/index.dart';
import 'package:whale/pages/im/model/msg.dart';
import 'package:whale/pages/im/util/image.dart';
import 'widgets.dart';

// ignore: must_be_immutable
class IMVoice extends IMCell {
  IMVoice({super.key, required super.md}) {
    m = md.msg as ImMsgVoiceModel;
  }
  late ImMsgVoiceModel m;
  double getVoiceWidth() {
    var mw = 200.r;
    var ms = 20;
    if (m.seconds > ms) {
      return mw;
    }
    Get.log(m.seconds.toString());
    return max(mw / ms * m.seconds * 1.2, 40.r);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImController>(
        id: m.type.name,
        builder: (context) {
          return InkWell(
            onTap: () {
              m.isPlaying = !m.isPlaying;
              controller.update([m.type.name]);
            },
            child: Container(
              padding: const EdgeInsets.fromLTRB(5, 2, 3, 2),
              width: getVoiceWidth(),
              height: 40.r,
              child: Row(
                children: [
                  SizedBox(
                    height: 20.r,
                    child: m.isPlaying
                        ? FrameImage(
                            imgs: List.generate(
                              3,
                              (index) => 'chat_bubble_voice_ripple0$index.png'
                                  .imImgPathCell,
                            ),
                          )
                        : Image.asset(
                            'chat_bubble_voice_ripple00.png'.imImgPathCell),
                  ),
                  const SizedBox(
                    width: 5,
                  ),
                  Text(
                    '${m.seconds}″',
                    style: TextStyle(
                      color: const Color(0xff333333),
                      fontSize: 13.sp,
                    ),
                  )
                ],
              ),
            ),
          );
        });
  }
}
